;
(function(ns) {
    ZYCLib.RegNameSpace(ns).ChangePwd = (function () {
        var self;
        var init = function(jsonData) {
            self = this;
            self.domain = jsonData.domain;
            self.action = jsonData.action;
        };

        var CheckInput = function (id, message, regExp, ismacth) {
            var isValidated = ZYCLib.Text.InputValidator(id, regExp, ismacth);
            if (!isValidated) {
                ShowErrorMessage(message, 0, function () {
                    $("#" + id).focus();
                });
            }
            return isValidated;
        };

        $(".change-pwd-post-btn").on("click", function () {
            if (self.isClickChangePwdPostButton) return;
            self.isClickChangePwdPostButton = true;

            if (!CheckInput("txtPassword", "请输入密码")) return;
            if (!CheckInput("txtPassword", "密码格式不正确", "^[A-Za-z0-9]{6,16}$", true)) return;
            if (!CheckInput("txtConfirmPassword", "请再次输入密码")) return;

            var isValid = ZYCLib.UI.Form.Validate("frmChangePwd");
            if (!isValid) {
                self.isClickChangePwdPostButton = false;
                return;
            }
            var userName = $("#txtUserName").val();
            var smsMobile = $("#txtSMSMobile").val();

            var newPwd = $("#txtNewPwd").val(),
                validNewPwd = $("#txtValidNewPwd").val();
            if (newPwd === userName) {
                ZYCLib.UI.MessageBox.Alert("密码不能和用户名相同", 3);
                self.isClickChangePwdPostButton = false;
                return;
            }
            if (newPwd === smsMobile) {
                ZYCLib.UI.MessageBox.Alert("密码不能和手机号相同", 3);
                self.isClickChangePwdPostButton = false;
                return;
            }
            if (newPwd !== validNewPwd) {
                $(".err").show();
                self.isClickChangePwdPostButton = false;
                return;
            }

            var url = self.domain + self.action;
            var postData = ZYCLib.UI.Form.FormToJsonData("frmChangePwd");
            ZYCLib.Web.JsonAjax.MvcRequest(url, postData, "post", true, false, function (result, jsonData) {
                self.isClickChangePwdPostButton = false;
                if (jsonData.ErrorCode === 0) {
                    ZYCLib.UI.MessageBox.Alert("修改密码成功。", 3, function () {
                        var params =
                            {
                                CommandName: "UserSignService/SignOut",
                                Data: {}
                            };
                        ZYCLib.Web.JsonAjax.NPostData(params.CommandName, params.Data,
                            function (jsondata) {
                                if (jsondata != null) {
                                    if (jsondata.ErrorCode === 0) {
                                        ZYCLib.CrossDomain.SingleSignOn.SignOut(function () {
                                            var oldUrl = window.location.href;
                                            window.location.href = "https://secure" + ZYCLib.Domain + "/signin/login?returnurl=" + encodeURIComponent(oldUrl);
                                        });
                                    }
                                }
                            }
                        );
                    });
                }
                else {
                    ZYCLib.UI.MessageBox.Alert(jsonData.ErrorMessage);
                }
            });

        });

        $("#txtValidNewPwd").on("change", function () {
            $(".err").hide();
            var _self = $(this),
                validNewPwd = _self.val(),
                newPwd = $("#txtNewPwd").val();
            if (newPwd !== validNewPwd)
                $(".err").show();
        });

        return {
            Init: init
        }
    })();
})("Page");
